package concurrence.lock;

import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockDemo {
    public static void main(String[] args) {
        Phone2 phone2 = new Phone2();
        new Thread(phone2, "t1").start();
        new Thread(phone2, "t2").start();
    }
}

class Phone2 implements Runnable {
    ReentrantLock reentrantLock = new ReentrantLock();
    public void getLock() {
        reentrantLock.lock();
        try {
            System.out.println(Thread.currentThread().getName()+"  getLock");
            setLock();
        } finally {
             reentrantLock.unlock();
        }

    }
    public void setLock() {
        reentrantLock.lock();
        try {
            System.out.println(Thread.currentThread().getName()+"  setLock");
        }finally{
            reentrantLock.unlock();
        }

    }
    @Override
    public void run() {
        getLock();
    }
}
